/*
 * Copyright (c) 2001, 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.core.internal.api;

import java.util.Map;

import net.sourceforge.perforce.api.IError;
import net.sourceforge.perforce.api.IPerforceCommand;
import net.sourceforge.perforce.api.internal.Error;
import net.sourceforge.perforce.api.internal.Spec;
import net.sourceforge.perforce.api.internal.SpecData;
import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.api.IChangeResult;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;

/**
  * ClientUser object for the 'p4 change' command.
  * 
  * @version 	$Revision: 45 $
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class Change extends AbstractPerforceCommand implements IPerforceCommand
{
	// --------------------------------------------------------------------------- Private Variables
	
	/** Stores the result of the operation. */
	private Result result;
	/** Stores the change specification. */
	private String changeSpec;

	// --------------------------------------------------------------- Public Static Factory Methods

	/** Convenience method for creating instances of Change.
	  * 
	  * @param changeSpec input parameter to the commands.
	  * @return the Change instance.
	  */
	public static final Change newInstanceForOutput(String changeSpec)
	{
		MultiStatus status = 
			new MultiStatus(PerforceCorePlugin.ID, IStatus.OK, "", null); 
			
		return new Change(status, changeSpec);
	}	

	/** Convenience method for creating instances of Change.
	  * 
	  * @return Change object used for creating a changelist.
	  */
	public static final Change newInstanceForInput()
	{
		MultiStatus status = 
			new MultiStatus(PerforceCorePlugin.ID, IStatus.OK, "", null); 
			
		return new Change(status);
	}
	
	// ------------------------------------------------------------------------ Private Constructors
	
	/** Constructor for Change.
	  * 
	  * @param status the MultiStatus.
	  * @param changeSpec the change specifiaction.
	  */
	private Change(MultiStatus status, String changeSpec)
	{
		super(status);
		this.changeSpec = changeSpec;
	}

	/** Constructor for Change.
	  * 
	  * @param status the multi status.
	  */
	private Change(MultiStatus status)
	{
		super(status);
	}

	// -------------------------------------------------------- Public Methods (IClientUserDelegate)

	/** @see IClientUserDelegate#inputData() */
	public String inputData(IError error)
	{
		return changeSpec;
	}

	/** @see IClientUserDelegate#outputStat(Map) */
	public void outputStat(Map strDict)
	{
		if (strDict.containsKey("specdef") && strDict.containsKey("data")) 
		{
			Error error = new Error();
			
			result = new Result();
			result.getChangeSpecDefinition().decode((String) strDict.get("specdef"), error); 
			result.getChangeSpecDefinition().parse(
				(String) strDict.get("data"), result.getChangeSpecData(), error, 0);
			
			if (error.test())
			{
				getMultiStatus().merge(new Status(IStatus.ERROR, PerforceCorePlugin.ID, 
					error.getGeneric(), error.format(IError.EF_PLAIN), null));
			}

			error.dispose();
		}
	}

	/** @see IClientUserDelegate#outputInfo(char, java.lang.String) */
	public void outputInfo(char level, String infoString) 
	{
	}	

	// ----------------------------------------------------------- Public Methods (IPerforceCommand)

	/** @see IPerforceCommand#getCommand() */
	public String getCommand()
	{
		return "change"; //$NON-NLS-1$
	}
	
	/** @see IPerforceCommand#getStatus() */
	public IStatus getStatus()
	{
		return getMultiStatus();
	}

	/** @see IPerforceCommand#isHidingFromConsole() */
	public boolean isHidingFromConsole()
	{
		return false;
	}

	// ------------------------------------------------------------------------------ Public Methods

	/** Returns the result of the operation.
	  * 
	  * @return the result.
	  */
	public IChangeResult getResult()
	{
		return result;
	}	

	// ---------------------------------------------------------------------- Private Nested Classes

	/** Result object that is returned as a result of the operation. */	
	private static class Result implements IChangeResult
	{
		// ----------------------------------------------------------------------- Private Variables
		
		/** Stores the changelist specificaiton. */
		private Spec spec = new Spec();
		/** Stores the data of the specification. */
		private SpecData specData = new SpecData();

		// -------------------------------------------------------------------------- Public Methods
		
		/** @see IChangeResult#getChangeSpecData() */
		public SpecData getChangeSpecData() 
		{ 
			return specData; 
		}

		/** @see IChangeResult#getChangeSpecDefinition() */
		public Spec getChangeSpecDefinition() 
		{ 
			return spec; 
		}
	}
}
